
const yz = require('../comm/yz');
var moment = require("moment");
var iz = require('iz');
var math = require("mathjs");
var clType = require("../dict/clothesSizeType")
exports.getStatus = function (statusId) {

    var status = null;
    this.statusArray().forEach(function (element) {

        if (element.id == statusId) {
            status = element
            return false;
        }
    }, this);

    return status


};



exports.statusArray = function (params) {

    return [
        {
            id: 100,
            name: "未接受"

        }, {
            id: 200,
            name: "已接受"

        }, {
            id: 300,
            name: "已完成"

        }
    ]

}


//工艺字典
exports.technologyArray = function () {


    return [
        {
            id: "100",
            name: "剪裁说明"
        },
        {
            id: "200",
            name: "粘衬说明"
        },
        {
            id: "300",
            name: "缝制工艺"
        },
        {
            id: "400",
            name: "质检要求"
        },
        {
            id: "500",
            name: "后道"
        },
        {
            id: "600",
            name: "自定义"
        }

    ]

}

exports.getTechnologyName = function (technologyId) {
    // body...
    var technologyName = "";
    this.technologyArray().forEach(function (element) {
        if (element.id == technologyId) {
            technologyName = element.name
        }
    }, this);

    return technologyName

};


exports.unitArray = function () {

    return [
        {
            id: "100",
            name: "米"
        },
        {
            id: "200",
            name: "码"
        },
        {
            id: "300",
            name: "件"
        },
        {
            id: "400",
            name: "个"
        },
        {
            id: "500",
            name: "条"
        },
        {
            id: "600",
            name: "克"
        },
        {
            id: "700",
            name: "套"
        }
    ]
}

//收款明细的状态
exports.settlementStatusArray = function () {
    return [

        {
            id: 0,
            name: "未收款"

        },
        {
            id: 100,
            name: "等待收款"

        },
        {
            id: 200,
            name: "部分到账"

        },
        {
            id: 300,
            name: "全部到账"
        }

    ]
}



exports.settlementClassArray = function () {
    // body...
    return [

        {
            id: "0",
            name: "样衣制作款"

        },
        {
            id: "100",
            name: "大货制作款"

        },
        {
            id: "200",
            name: "大货制作-中款"

        },
        {
            id: "300",
            name: "大货制作-尾款"
        },
        {
            id: "zdy",
            name: "自定义"
        }

    ]
};

exports.getSettlementStatusName = function (statusId) {
    var statusName = ""
    statusId = parseInt(statusId)
    this.settlementStatusArray().forEach(function (element) {
        if (element.id == statusId) {
            statusName = element.name;
        }
    }, this);
    return statusName
    // body...
};


exports.getClassName = function (classId) {
    var className = "";
    this.classArray().forEach(function (element) {
        if (element.id == classId) {
            className = element.name

            return false
        }
    }, this);

    return className
};

exports.classArray2 = function () {
    var array = this.classArray();
    return array.splice(1, 2);  //删除前两个, 从面辅料开始
}


exports.classArray = function () {


    return [

        {

            name: "起始",
            id: "root"
        },
        {

            name: "默认",
            id: "default"
        },


        {
            name: "面辅料",
            id: "100"

        },
        {
            name: "工价",
            id: "200"

        },
        {
            name: "制版",
            id: "300"

        },
        {
            name: "工艺",
            id: "400"

        },
        {
            name: "裁片",
            id: "500"

        },
        {
            name: "车缝",
            id: "600"

        },
        {
            name: "后道",
            id: "700"

        },
        {
            name: "交货",
            id: "800"

        },
        {
            name: "结算",
            id: "900"

        }
    ]

}

exports.WageClass = function () {
    // body...
    return [
        {
            id: "100",
            name: "制版费"
        }, {
            id: "200",
            name: "样衣费"
        }, {
            id: "300",
            name: "放码费"
        }, {
            id: "400",
            name: "再样费"
        }, {
            id: "500",
            name: "印花费"
        }, {
            id: "600",
            name: "洗水加工费"
        }, {
            id: "700",
            name: "裁床加工费"
        },

        {
            id: "1300",
            name: "车缝加工费"
        },
        {
            id: "800",
            name: "后道加工费"
        }, {
            id: "900",
            name: "管理费"
        }, {
            id: "1000",
            name: "物流费"
        }, {
            id: "1100",
            name: "优惠费用"
        },
        // {
        //     id: "1200",
        //     name: "制版费"
        // },

        {
            id: "zdy",
            name: "自定义"
        }
    ]
};


exports.fmtTask = function (task) {

    if (!task) {
        task = {};
    }
    if (!task.name) {
        task.name = '无'

    }
    task.createTime = yz.IsNullThen(task.createTime, new Date());
    task.changeTime = yz.IsNullThen(task.changeTime, task.createTime);
    task.startTime = yz.IsNullThen(task.createTime, new Date());
    task.endTime = yz.IsNullThen(task.createTime, new Date());
    task.taskClassId = yz.IsNullThen(task.taskClassId, "");
    task.nodeId = yz.IsNullThen(task.nodeId, "");
    task.detail = yz.IsNullThen(task.detail, []);
    task.managers = yz.IsNullThen(task.managers, [])
    task.members = yz.IsNullThen(task.members, [])

    task.detail = this.fmtDetal(task.detail, task.taskClassId);




    if (task.taskClassId == "200") {
        //制版有两个detail
        task.detail2 = this.fmtDetal(task.detail2, task.taskClassId);
    }





    this.countDetal(task);

    return task;

}

exports.fmtDetal = function (detail, taskClassId) {

    var _detail = [];
    if (yz.isArray(detail)) {
        detail.forEach(function (item) {



            if (item.taskClassId == taskClassId) {
                //如果detail明细的class等于当前主任务的class  这里必须相等, 如果不相等, 就删除了. 一般不会出现不相等的情况, 除非任务在创建之后有更改了任务class. 除了技术调整数据之外, 没有理由这样做
                item = this.fmtDetailItem(item, taskClassId);
                _detail.push(item);
            }





        }, this);
    }



    return _detail
}

exports.countDetal = function (task) {

    switch (task.taskClassId) {
        case "100":  //面辅料 

        case "200": //工价
            var amount = 0
            task.detail.forEach(function (item) {

                item.amount = math.chain(item.price).multiply(item.num).round(2).value;

                amount = math.chain(item.amount).add(amount).round(2).value;


            }, this);
            task.amount = amount;
            break;
        default:

    }

}

//裁片 车缝  后道 交货  结算的detail列表过滤格式化
exports.fmtSizeDetail = function (task, customerOrder) {



    switch (task.taskClassId) {
        case "500":
        case "600":
        case "700":
        case "800":
            var size = customerOrder[task.flowClassId]

            var list = [];
            size.sizeIdArray.forEach(function (sizeId) {


                size.data.forEach(function (colorItem) {

                    colorItem.sizeArray.forEach(function (sizeItem) {

                        if (sizeItem.id == sizeId) {
                            var item = {};
                            item = this.fmtDetailItem(item, task.taskClassId)



                            item.colorName = colorItem.colorName
                            item.sizeId = sizeId

                            task.detail.forEach(function (oldItem) {
                                if (oldItem.colorName == item.colorName && oldItem.sizeId == item.sizeId) {

                                    item = oldItem
                                }

                            }, this);

                            item.num = sizeItem.num   //下单数量
                            // if (item.num == 0) {
                            //     return false   不在限制0下单就没有数据
                            // }
                            switch (task.taskClassId) {
                                case "500":  //裁片

                                    //   item.sectionNum = yz.IsNullThen(size.sectionNum, "");  //裁片数量
                                    item.sectionDoneNum = yz.IsNullThen(sizeItem.sectionDoneNum, 0);     //完成裁片数量
                                    item.modelNo = size.modelNo
                                    list.push(item)
                                    break;
                                case "600": //车缝

                                    item.sectionDoneNum = yz.IsNullThen(sizeItem.sectionDoneNum, 0);     //完成裁片数量
                                    item.sewDoneNum = yz.IsNullThen(sizeItem.sewDoneNum, 0);     //完成车缝数量
                                    item.modelNo = size.modelNo
                                    list.push(item)
                                    break;


                                case "700":  //后道
                                    item.sewDoneNum = yz.IsNullThen(sizeItem.sewDoneNum, 0);     //完成车缝数量
                                    item.completeDoneNum = yz.IsNullThen(sizeItem.completeDoneNum, 0);     //完成后道数量
                                    item.modelNo = size.modelNo
                                    list.push(item)
                                    break
                                case "800":   //交货
                                    item.completeDoneNum = yz.IsNullThen(sizeItem.completeDoneNum, 0);     //完成后道数量
                                    item.clothesDoneNum = item.completeDoneNum  //目前先让成衣数量等于后道数量, 等加入质检环节后再行扩展

                                    item.cdeliverDoneNum = yz.IsNullThen(sizeItem.cdeliverDoneNum, 0);     //已交付数量
                                    item.modelNo = size.modelNo
                                    list.push(item)
                                    break


                                default:

                                    break;
                            }



                        }

                    }, this);



                }, this);

            }, this);

            return list;


        default:

            return task.detail

    }



};


exports.fmtDetailItem = function (item, taskClassId) {
    // body...
    item.taskClassId = taskClassId

    item.id = yz.IsNullThen(item.id, yz.getId());     //下单数量(件)
    item.createTime = new Date(yz.IsNullThen(item.createTime, new Date()));     //下单数量(件)
    item.memo = yz.IsNullThen(item.memo, "");  //备注
    switch (taskClassId) {
        case "100": //面辅料
            item.menfu = yz.IsNullThen(item.menfu, 0);     //数量
            item.unitId = yz.IsNullThen(item.unitId, "");  //单位编号

            break
        case "200": //工价
            item.num = yz.IsNullThen(item.num, 0);     //数量
            item.price = yz.IsNullThen(item.price, 0);     //数量
            item.amount = math.chain(item.price).multiply(item.num).round(2).value;  //金额
            item.wageClass = yz.IsNullThen(item.wageClass, { name: "自定义", id: "zdy" });  //编码
            break
        case "300":  //制版
            item.part = yz.IsNullThen(item.part, "");  //部位
            item.method = yz.IsNullThen(item.method, "");  //量法
            item.errorRange = yz.IsNullThen(item.errorRange, 0);     //误差
            item.dangRange = yz.IsNullThen(item.errorRange, 0);     //裆差
            break

        case "400":  //工艺


            item.id = yz.IsNullThen(item.id, moment().utcOffset(-8).format("YYMMDDHHmmssSSS"))

            item.technologyId = yz.IsNullThen(item.technologyId, "100");  //工艺id

            item.technologyName = yz.IsNullThen(item.technologyName, "");  //工艺名称. 如果id是600的话, 是自定义的, 这时候就要用到自定义名称


            item.list = yz.IsNullThen(item.list, []);  //明细
            item.list.forEach(function (element) {
                element.imgUrl = yz.IsNullThen(element.imgUrl, "");  //图片路径
                element.list = yz.IsNullThen(element.list, []);  //图片路径
                element.part = yz.IsNullThen(element.part, "");  //部位
                element.memo = yz.IsNullThen(element.memo, "");  //描述

                element.createTime = yz.IsNullThen(element.createTime, new Date());  //单位编号
            }, this);
            break

        case "500":  //裁片

            item.sectionDoneNum = yz.IsNullThen(item.sectionDoneNum, 0);     //完成裁片数量
            break

        case "600":  //车缝

            item.sewDoneNum = yz.IsNullThen(item.sewDoneNum, 0);     //完成车缝数量
            break

        case "700":  //后道


            item.completeDoneNum = yz.IsNullThen(item.completeDoneNum, 0);     //完成后道数量
            break

        case "800":  //交货  
            item.clothesDoneNum = yz.IsNullThen(item.completeDoneNum, 0);     //成衣数量 目前等于后道数量
            item.cdeliverDoneNum = yz.IsNullThen(item.cdeliverDoneNum, 0);     //已交付数量
            break

        case "900":  //结算
            item.name = yz.IsNullThen(item.name, "");     //收款明细名称
            item.sendMemberId = yz.IsNullThen(item.sendMemberId, "");     //提交收款人
            item.amount = yz.IsNullThen(item.amount, 0);     //结算金额

            item.reAmount = yz.IsNullThen(item.reAmount, 0);     //收款金额

            item.statusId = yz.IsNullThen(item.statusId, 0);     //收款状态 
            item.collectionTime = new Date(yz.IsNullThen(item.collectionTime, new Date()));     //收款时间
            item.paymentTime = new Date(yz.IsNullThen(item.paymentTime, new Date()));     //到账时间
            break

        case "200" || "100": // 共同节点
            item.num = yz.IsNullThen(item.num, 0);     //数量
            item.price = yz.IsNullThen(item.price, 0);   //单价
            item.amount = yz.IsNullThen(item.amount, 0);  //金额

            item.name = yz.IsNullThen(item.name, "");  //名称
            break

        case "500" || "600" || "700" || "800" || "900":
            item.modelNo = yz.IsNullThen(item.modelNo, "");  //款号
            item.color = yz.IsNullThen(item.color, "");  //颜色
            item.sizeId = yz.IsNullThen(item.sizeId, "");  //尺码编号 S L X XXL的id
            item.orderNum = yz.IsNullThen(item.orderNum, 0);     //下单数量(件)



            break
        default:



    }

    return item;
};

exports.getModelNo = function (task) {
    // body...

    var re = {
        proof: "-",
        bulk: "-"
    }
    try {


        re.proof = task.customerOrder["proof"].modelNo

    } catch (e) {
        return ""
    }

    try {
        re.bulk = task.customerOrder["bulk"].modelNo

    } catch (e) {

    }

    if (re.proof == "" || !re.proof) {
        re.proof = "-"
    }
    if (re.bulk == "" || !re.bulk) {
        re.bulk = "-"
    }

    return re

};







